/**
 * 
 */

package com.chengyu.eyc.utils;

import java.net.InetAddress;
import java.net.UnknownHostException;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.util.WebUtils;

import lombok.extern.slf4j.Slf4j;

/**
 * @author kevin
 *
 */
@Slf4j
public final class WebUtil extends WebUtils {

    /**
     * 逗号
     */
    private static final char COMMA = ',';

    private static final String IP_UNKNOWN = "unknown";

    public static String getClientIp(HttpServletRequest request) {
        String ipAddress = request.getHeader("x-forwarded-for");
        if (StringUtils.isNotBlank(ipAddress) && !"unknown".equalsIgnoreCase(ipAddress) && ipAddress.indexOf(COMMA) != -1) {
            // 多次反向代理后会有多个ip值，第一个ip才是真实ip
            ipAddress = ipAddress.split(",")[0];
        }
        if (StringUtils.isBlank(ipAddress) || StringUtils.equalsIgnoreCase(IP_UNKNOWN, ipAddress)) {
            ipAddress = request.getHeader("Proxy-Client-IP");
        }
        if (StringUtils.isBlank(ipAddress) || StringUtils.equalsIgnoreCase(IP_UNKNOWN, ipAddress)) {
            ipAddress = request.getHeader("WL-Proxy-Client-IP");
        }
        if (StringUtils.isBlank(ipAddress) || StringUtils.equalsIgnoreCase(IP_UNKNOWN, ipAddress)) {
            ipAddress = request.getHeader("HTTP_CLIENT_IP");
        }
        if (StringUtils.isBlank(ipAddress) || StringUtils.equalsIgnoreCase(IP_UNKNOWN, ipAddress)) {
            ipAddress = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (StringUtils.isBlank(ipAddress) || StringUtils.equalsIgnoreCase(IP_UNKNOWN, ipAddress)) {
            ipAddress = request.getHeader("X-Real-IP");
        }

        if (StringUtils.isBlank(ipAddress) || IP_UNKNOWN.equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getRemoteAddr();
            if (StringUtils.equals(ipAddress, "127.0.0.1") || StringUtils.equals(ipAddress, "0:0:0:0:0:0:0:1")) {
                /*根据网卡取本机配置的IP  */
                InetAddress inet = null;
                try {
                    inet = InetAddress.getLocalHost();
                } catch (UnknownHostException e) {
                    log.error(e.getMessage(), e);
                }
                if (inet != null) {
                    ipAddress = inet.getHostAddress();
                }

            }
        }
        /*对于通过多个代理的情况，第一个IP为客户端真实IP,多个IP按照','分割  */
        if (ipAddress != null && ipAddress.length() > 15 && ipAddress.indexOf(COMMA) >= 0) { /*"***.***.***.***".length() = 15*/
            ipAddress = ipAddress.substring(0, ipAddress.indexOf(','));
        }
        return ipAddress;
    }

    public static boolean isAjaxRequest(HttpServletRequest req) {
        return req.getHeader("X-Requested-With") != null && "XMLHttpRequest".equals(req.getHeader("X-Requested-With"));
    }

    public static boolean isJsonAcceptRequest(HttpServletRequest req) {
        String accept = req.getHeader("Accept");
        return accept != null && accept.contains("json");
    }

}
